package com.zz.controller;

import com.zz.dao.UserDao;
import com.zz.pojo.User;
import com.zz.utils.RandomValidateCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Copyright Zz
 * FileName: UserController
 * Author:   Zz
 * Date:     2023/6/217:06
 * Description:用户Controller
 * hISTORY:
 * <author>  <time>     <version>    <desc>
 * 作者姓名   修改时间       版本号       描述
 */
@Controller
@Slf4j
public class UserController {

    @Autowired
    UserDao userDao;

    @Autowired
    RandomValidateCode randomValidateCode;

    @PostMapping("login")
    @ResponseBody
    public int Login(@RequestBody User user, HttpServletRequest request) {
        HttpSession session = request.getSession();
        Integer time = (Integer) session.getAttribute("time");
        if (time != null && time.intValue() == 3) {
            return -2;// 尝试次数已达三次
        }
        String validateCode = (String) session.getAttribute("RandomValidateCode");
        if (!validateCode.equalsIgnoreCase(user.getValidateCode())) {
            return -1;// 验证码错误
        }

        try {
            String s = userDao.queryUser(user.getUserName());
            if (s != null) {


                int num = userDao.login(user);
                if (num != 1) {
                    if (time == null) {
                        session.setAttribute("time", 1);
                    } else {
                        session.setAttribute("time", time + 1);
                    }
                } else {
                    session.removeAttribute("time");
                }
                return num;
            } else {
                return 2;// 用户名不存在
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @PostMapping("signUp")
    @ResponseBody
    public int signUp(@RequestBody User user, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String validateCode = (String) session.getAttribute("RandomValidateCode");
        if (!validateCode.equalsIgnoreCase(user.getValidateCode())) {
            return -1;// 验证码错误
        }

        try {
            String b = userDao.queryUser(user.getUserName());
            if (b != null)
                return 2;// 用户名已存在
            else return userDao.signUp(user);// 注册是否成功
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }


    @GetMapping("validateCode123")
    @ResponseBody
    public int getRandomValidateCode(HttpServletRequest request, HttpServletResponse response) {
        try {
            randomValidateCode.getRandcode(request, response);
        } catch (Exception e) {
            log.info(e.toString());
            return 0;
        }
        return 1;
    }
}
